import tkinter as tk
from tkinter import messagebox, filedialog
import threading
import datetime

def do_nothing():
    pass

def popup():
    messagebox.showinfo("Popup", "Hello, this is a popup message!")

def new_file():
    file_path = filedialog.asksaveasfilename(
        defaultextension=".txt",
        filetypes=[("Text Files", "*.txt"), ("All Files", "*.*")],
        title="保存文件"
    )
    if file_path:
        with open(file_path, 'w') as f:
            pass
        messagebox.showinfo("New File", f"New file created at:\n{file_path}")

def open_editor_window():
    editor_window = tk.Toplevel(root)
    editor_window.title("文本编辑器")

    text_editor = tk.Text(editor_window)
    text_editor.pack(expand=True, fill='both')

    def save_text():
        file_path = filedialog.asksaveasfilename(
            defaultextension=".txt",
            filetypes=[("Text Files", "*.txt"), ("All Files", "*.*")],
            title="保存文件"
        )
        if file_path:
            with open(file_path, 'w') as f:
                text = text_editor.get("1.0", tk.END)
                f.write(text)
            messagebox.showinfo("文件保存", f"文件保存成功:\n{file_path}")

    save_button = tk.Button(editor_window, text="保存", command=save_text)
    save_button.pack()

def set_reminder():
    reminder_window = tk.Toplevel(root)
    reminder_window.title("设置提醒")

    def show_reminder():
        message = reminder_entry.get()
        reminder_time = reminder_time_entry.get()
        if message and reminder_time:
            try:
                # 解析用户输入的时间
                reminder_datetime = datetime.datetime.strptime(reminder_time, "%Y-%m-%d %H:%M:%S")
                # 计算当前时间和提醒时间的时间差
                delta = (reminder_datetime - datetime.datetime.now()).total_seconds()
                if delta > 0:
                    # 启动定时器，到时提醒
                    reminder_timer = threading.Timer(delta, lambda: messagebox.showinfo("提醒", message))
                    reminder_timer.start()
                    messagebox.showinfo("设置成功", f"提醒将在 {reminder_time} 提醒。")
                    reminder_window.destroy()
                else:
                    messagebox.showerror("错误", "提醒时间必须是未来的时间。")
            except ValueError:
                messagebox.showerror("错误", "请输入正确的时间格式：YYYY-MM-DD HH:MM:SS")

    reminder_label = tk.Label(reminder_window, text="输入提醒消息:")
    reminder_label.pack(pady=10)

    reminder_entry = tk.Entry(reminder_window, width=30)
    reminder_entry.pack(pady=10)

    reminder_time_label = tk.Label(reminder_window, text="输入提醒时间（YYYY-MM-DD HH:MM:SS）:")
    reminder_time_label.pack(pady=10)

    reminder_time_entry = tk.Entry(reminder_window, width=30)
    reminder_time_entry.pack(pady=10)

    remind_button = tk.Button(reminder_window, text="设置提醒", command=show_reminder)
    remind_button.pack(pady=10)

def start_reminder():
    set_reminder()

root = tk.Tk()
root.geometry("300x200")
menu_bar = tk.Menu(root)
root.config(menu=menu_bar)

file_menu = tk.Menu(menu_bar, tearoff=0)
menu_bar.add_cascade(label="文件", menu=file_menu)
file_menu.add_command(label="新建", command=new_file)
file_menu.add_command(label="打开", command=open_editor_window)
file_menu.add_separator()
file_menu.add_command(label="退出", command=root.quit)

reminder_menu = tk.Menu(menu_bar, tearoff=0)
menu_bar.add_cascade(label="提醒", menu=reminder_menu)
reminder_menu.add_command(label="设置提醒", command=start_reminder)

help_menu = tk.Menu(menu_bar, tearoff=0)
menu_bar.add_cascade(label="帮助", menu=help_menu)
help_menu.add_command(label="关于", command=do_nothing)

popup_btn = tk.Button(root, text="弹出消息", command=popup)
popup_btn.pack(pady=20)

root.mainloop()